package com.smh.a04UDPDemo3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ReceiveMessageDemo3 {
    public static void main(String[] args) throws IOException {

        MulticastSocket ms = new MulticastSocket(10000);

        //这里的组和前两个不一样,所以不会收到数据
        InetAddress address = InetAddress.getByName("224.0.0.2");
        ms.joinGroup(address);

        byte[] bytes = new byte[1024];

        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        ms.receive(dp);

        String hostAddress = dp.getAddress().getHostAddress();
        byte[] data = dp.getData();
        int length = dp.getLength();
        int port = dp.getPort();

        System.out.println("接收到的数据" + new String(data, 0, length));
        System.out.println("该数据是从" + hostAddress + "这台电脑中的" + port + "端口发送的");

        //释放资源
        ms.close();


    }
}
